<?php
//第一步：透过代理获取用户真实的IP地址
header("Content-Type:text/html;charset=utf-8");
$theip=getip();
function getIP()
{
    static $realip;
    if (isset($_SERVER)){
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
            $realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $realip = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $realip = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")){
            $realip = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }

    return $realip;
}

function numIP($ip) { 
$ip=explode(".", $ip); 
return $ip[0]*255*255*255+$ip[1]*255*255+$ip[2]*255+$ip[3];
} 


$theip=numIP($theip);

$acceptedIPs=array( 
array("119.57.24.1" , "119.57.24.255"),
array("111.161.8.1" , "111.161.8.255"),
array("114.251.124.1" , "114.251.124.255"),
array("124.193.192.1" , "124.193.192.255"),
array("125.39.68.1" , "125.39.68.255"),
//如果要再加IP段就在这里加：array("起始IP" , "结束IP"), 
); 

$redirect="1"; 
for ($ind=0; $ind<count($acceptedIPs); $ind++) { 
$ipBegin=numIP($acceptedIPs[$ind][0]); 
$ipEnd=numIP($acceptedIPs[$ind][1]); 
    if ($theip>=$ipBegin && $theip<=$ipEnd) {
		$redirect="0"; 
        break;
    }
}
//echo $redirect;
if ($redirect=="1") {
	header('HTTP/1.1 404 Not Found');
	header('Location: /404.shtml');
}
?>